//最⻓重复⼦数组（medium）: https://leetcode.cn/problems/maximum-length-of-repeated-subarray/description/
class Solution
{
public:

	int findLength(vector<int>& nums1, vector<int>& nums2)
	{
		int m = nums1.size(), n = nums2.size();
		vector<vector<int>> dp(m + 1, vector<int>(n + 1));
		int ret = 0;
		for (int i = 1; i <= m; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (nums1[i - 1] == nums2[j - 1]) // ⼀遍填表，⼀遍更新最⼤值
				{
					dp[i][j] = dp[i - 1][j - 1] + 1, ret = max(ret, dp[i][j]);
				}
			}
		}
			
		return ret;
	}
};

//01 背包（medium）: https://www.nowcoder.com/practice/fd55637d3f24484e96dad9e992d3f62e?tpId=230&tqId=2032484&ru=/exam/oj&qru=/ta/dynamic-programming/question-ranking&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196
#include <iostream>
#include <string.h>
using namespace std;
const int N = 1010;
int n, V, v[N], w[N];
int dp[N][N];
int main()
{

	// 读⼊数据
	cin >> n >> V;
	for (int i = 1; i <= n; i++)
	{
		cin >> v[i] >> w[i];
	}
	
	// 解决第⼀问
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= V; j++) // 修改遍历顺序
		{
			dp[i][j] = dp[i - 1][j];
			if (j >= v[i])
				dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
		}
	}
		

	cout << dp[n][V] << endl;
	// 解决第⼆问
	memset(dp, 0, sizeof dp);
	for (int j = 1; j <= V; j++)
	{
		dp[0][j] = -1;
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= V; j++) // 修改遍历顺序
		{
			dp[i][j] = dp[i - 1][j];
			if (j >= v[i] && dp[i - 1][j - v[i]] != -1)
			{
				dp[i][j] = max(dp[i][j], dp[i - 1][j - v[i]] + w[i]);
			}
		}
	}

	cout << (dp[n][V] == -1 ? 0 : dp[n][V]) << endl;
	return 0;
}